// pages/playlist/playlist.js
import {getUserPlaylist} from "../../store/user";
import {getListDetail} from "../../lib/music";
import {getMusicIdx, getPlayList, setMusicIdx, setPlayList} from "../../store/playlist";
import {audioCtx} from "../../store/constants";
import {setPlay} from "../../store/play";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lid: '',
        musics: [], // 歌单下的所有歌曲
        listInfo: {} // 歌单信息
    },

    // 播放
    playMusic(e) {
        let obj = e.currentTarget.dataset.obj
        audioCtx.src = e.currentTarget.dataset.obj.src
        audioCtx.play()
        setPlay({
            title: obj.title,
            singer: obj.singer,
            coverImgUrl: obj.coverImgUrl,
            currentTime: '00:00',
            duration: '00:00',
            percent: 0
        })
        // console.log(getPlay())
        setMusicIdx(e.currentTarget.dataset.idx)
        console.log(getMusicIdx())
        // 更换播放列表
        setPlayList(this.data.musics)
        console.log(getPlayList())
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 获取歌单id
        console.log(options.lid)
        this.setData({lid: options.lid})
        getListDetail(this.data.lid, (res) => {
            console.log(res)
            let {list, mids, musics} = res.data
            this.setData({
                listInfo: list, musics
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})